package forms;

import org.apache.struts.action.*;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

public class studentsinfoform extends ActionForm{

	private static final long serialVersionUID = 1L;
	
	private String first = "";
	private String middle = "";
	private String last = "";
	private String citizen = "";

	public String getCitizen() {
		return citizen;
	}
	public void setCitizen(String citizen) {
		this.citizen = citizen;
	}



	public String getFirst() {
		return first;
	}
	public void setFirst(String first) {
		this.first = first;
	}
	public String getMiddle() {
		return middle;
	}
	public void setMiddle(String middle) {
		this.middle = middle;
	}
	public String getLast() {
		return last;
	}
	public void setLast(String last) {
		this.last = last;
	}
	
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		setFirst("");
		setMiddle("");
		setLast("");
	}
	
	private boolean isInvalid(char[] word)
	{
		boolean invalid = false;
		for(int i = 0; i < word.length; i++ )
		{
			if( (word[i] >= 65 && word[i] <= 90) || (word[i] >= 97 && word[i] <= 122) )
			{
				invalid = false;
			}
			else
			{
				invalid = true;
			}
		}
		return invalid;
	}
	
	// check if first name, middle initial, and last name are provided and valid
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		if( first.equals("") )
		{
			errors.add("first", new ActionMessage("error.firstname.required"));
			setFirst("");
		}
		if( last.equals("") )
		{
			errors.add("last", new ActionMessage("error.lastname.required"));
			setLast("");
		}
		if( isInvalid(first.toCharArray()) )
		{
			errors.add("first", new ActionMessage("error.firstname.invalid"));
			setFirst("");
		}
		if( isInvalid(middle.toCharArray()) )
		{
			errors.add("middle", new ActionMessage("error.middle.invalid"));
			setMiddle("");
		}
		if( isInvalid(last.toCharArray()) )
		{
			errors.add("last", new ActionMessage("error.lastname.invalid"));
			setLast("");
		}

		return errors;
	}
}
